<{capture name="header"}>
<{css app="ome" src="ome.css"}>
<{/capture}>

<div class="tableform">
  <form method="post" action="index.php?app=ome&ctl=admin_consign&act=batchConsign" id="processSendForm">
  <input type="hidden" id="order_exists_refund" name="order_exists_refund" value="" />
    <h3>发货处理 <span class="num">(目前还有<em class="count" style="font-size:30px"> <{$num}></em>个发货单未发货)</span></h3>
    <div class="division">
      <table width="98%" cellspacing="0" cellpadding="0" border="0">
        <tbody>
          <tr>
            <td colspan="2">注：如果手动输入快递单号，完成后需按回车键，<br />在输入过程中请不要加入空格或其他符号！</td>
          </tr>
          <tr>
            <td width="330"><textarea id="delivery" style="font-size:14px; line-height:30px; padding:8px;" placeholder="批量扫描或输入快递单号" cols="40" rows="16" onfocus="if(this.value=='批量扫描或输入快递单号') this.value='';">批量扫描或输入快递单号</textarea></td>
            <td id="dataNode" class="red fb" style="font-size:30px"></td>
          </tr>
        </tbody>
      </table>
    </div>
  </form>
</div>

<{area inject=".mainFoot"}>
<div class="table-action">
  <{button type="button" id="btn_submit" label="确定"}>
</div>
<{/area}>
<script>

  (function(){
    $('delivery').addEvent('focus',function(e){
      try{$('btn_submit').set('disabled', '');}catch(e){}
    }).focus();
    $('btn_submit').addEvent('click',function(e){
        var value = $('delivery').value.replace(/^[\s\n\r]*|[\s\n\r]*$/,'').split(/[\n\r\s,]+/);
        if (value.length > 50){
        	new Dialog(new Element("div.tableform",{html:'<div id="delivery_max_limit" class="division">一次批量发货最多50个发货单!</div>'}),{
                title:'当前共'+value.length+'个发货单',
                width:250,
                height:80,
                modal:true,
                resizeable:false});
        	return;
        }
        if (value == ''){
            return MessageBox.error('请输入快递单号');
        }

        var tmp = $E('input[name="delivery_id"]') && $E('input[name="delivery_id"]').set('value',value.join(',').substring(-1)) || new Element('input[type=hidden][name="delivery_id"]').set('value',value.join(',').substring(-1)).inject($('processSendForm'));
        var val = encodeURIComponent(tmp.value);

        new Request({url:'index.php?app=ome&ctl=admin_consign&act=batchCheck',data:'delivery_id='+val,method:'POST',
        onRequest: function(){
            this.disabled = true;
          },
          onComplete: function(json){
            if (json != ''){
	        	json = JSON.decode(json);
	        	var refund_orders, is_confirm_delivery = false;
	        	if (json != ''){
		            json = json.filter(function(n){
		                if (n.order_exists_refund != ''){
		                	is_confirm_delivery = true;
		                	refund_orders = n.order_exists_refund;
		                }
		                return !n.order_exists_refund;
		            });
	        	}
            }
            if(json != ''){
              var msg = "快递单号存在异常状况：<br />";
              json.each(function(j){
            	  if (typeof(j.bn) != 'undefined'){
            		  msg += j.bn + ' &nbsp; ' + j.msg + '<br />';
            	  }
              });
              $('dataNode').set('html',msg);
              return MessageBox.error('单号有异常');
            }else {
                //判断订单是否申请退款
                if (is_confirm_delivery == true && refund_orders != ''){
                    $('order_exists_refund').set('value', refund_orders);
                    var url = 'index.php?app=ome&ctl=admin_consign&act=batch_delivery_confirm&delivery_type=batch';
                	new Dialog(url,{title:'批量发货确认',width:500,height:300})
                }else{
					$('dataNode').set('html','');
					new Dialog(new Element("div.tableform",{html:'<div id="notice" class="division">正在发货中,请稍候...</div>'}),{
		                title:'提示',
				        width:230,
				        height:80,
				        modal:true,
				        resizeable:false});
					
	                $('processSendForm').fireEvent('submit',e);
                }
            }

          }
        }).send();
    });

    $('processSendForm').store('target',{
        onRequest:function(){
    	   $('btn_submit').set('disabled', 'true');
        },
        onComplete:function(jsontext){
           $('notice').getParent('.dialog').retrieve('instance').close();
           var json = JSON.decode(jsontext);
           if (typeof(json.error) != 'undefined'){
        	   //显示发货失败消息
               if (json.msg.delivery_bn){
                   if (json.msg.delivery_succ == 'succ'){
                	  var msg = ' 以下发货单发货失败,其余发货成功!<br/>';
                   }else{
                	  var msg = ' 以下发货单发货失败!<br/>';
                   }
                   $('dataNode').show().set('html', msg + json.msg.delivery_bn);
               }
               $('btn_submit').set('disabled', '');
           }else{
               $('btn_submit').set('disabled', 'true');
           }
        }
    });

  })();
</script>
